package com.windows
{
	import com.bases.ImageButton;
	import com.bases.TLable;
	import com.bases.TPanel;
	import com.skin.SkinManage;
	
	import config.TConfig;
	
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	/**
	 * 弹出警告窗口 
	 * @author pethan
	 * 
	 */	
	public class AlertWindow extends TPanel
	{
		private var lblContet:TLable;
		private var btnSure:ImageButton;
		private var btnCancle:ImageButton;
		
		public static var YES:String='yes';
		public static var NO:String='no';
		
		public var yesFun:Function;
		public var noFun:Function;
		public var params:Array;
		/**
		 *  
		 * @param content 警告内容
		 * 
		 */		
		public function AlertWindow(content:String,buttons:Array)
		{
			super('提示', 290, 156, false, true);
			lblContet=new TLable();
			lblContet.text=content;
			lblContet.setFontSize(18);
			lblContet.multiline=true;
			lblContet.wordWrap=true;
			btnSure=new ImageButton(SkinManage.getInstance().getClass('OKBmp'),SkinManage.getInstance().getClass('OKOverBmp'),SkinManage.getInstance().getClass('OKOverBmp'));
			this.btnSure.addEventListener(MouseEvent.CLICK,onMouseSure);
			if(buttons!=null){
				if(buttons.indexOf(NO)!=-1){
					btnCancle=new ImageButton(SkinManage.getInstance().getClass('OKBmp'),SkinManage.getInstance().getClass('OKOverBmp'),SkinManage.getInstance().getClass('OKOverBmp'));
					this.btnCancle.addEventListener(MouseEvent.CLICK,onMouseCancel);
					this.addChild(btnCancle);
				}
			}
			this.addChilds(lblContet,btnSure);
			this.relayout();
			TConfig.getInstance().stage.stage.focus=this;
			TConfig.getInstance().stage.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
		}
		
		private function onKeyDown(e:KeyboardEvent):void{
			if(e.keyCode==TConfig.getInstance().ENTER_CODE){
				onMouseSure(null);
			}	
		}
		
		private function onMouseCancel(event:MouseEvent):void{
			if(this.noFun!=null){
				if(this.params!=null){
					noFun.call(null,this.params);
				}else{
					noFun.call();
				}
			}
			this.closeWindow();
		}
		
		private function onMouseSure(event:MouseEvent):void{
			if(this.params==null){
				if(yesFun!=null){
					yesFun.call();
				}
			}else{
				yesFun.call(null,this.params);
			}
			this.closeWindow();
		}
		
		private function closeWindow():void{
			TWindowsManager.getInstance().removeWindow(this);
			TConfig.getInstance().stage.stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
		}
		
		public function addSureFunction(fun:Function):void{
			yesFun=fun;
		}
		
		public function addCancelFunction(fun:Function):void{
			this.btnCancle.addEventListener(MouseEvent.CLICK,fun);
		}
		
		override protected function relayout():void{
			super.relayout();
			this.lblContet.setPos(30,50);
			this.lblContet.setSize(220,200);
			if(this.btnCancle!=null){
				this.btnSure.setPos(160,120);
				this.btnCancle.setPos(220,120);
			}else{
				this.btnSure.setPos(220,120);
			}
		}
		
		
		/**
		 * 弹出警告窗口(类拟Alert) 
		 * @param content 内容
		 * @param buttons 需要的按钮[AlertWindow.Yes,AlertWindow.NO]
		 * @param parent 父窗口
		 * @param closeHander 按确定执行的方法
		 * @return 弹出的窗口
		 */		
		public static function Show(content:String,buttons:Array=null,parent:Sprite=null,closeHander:Function=null,noFun:Function=null,array:Array=null):AlertWindow{
			var alertwindow:AlertWindow=new AlertWindow(content,buttons);
			if(closeHander!=null){
				alertwindow.addSureFunction(closeHander);
			}
			if(noFun!=null){
				alertwindow.noFun=noFun;
			}
			if(array!=null){
				//参数
				alertwindow.params=array;
			}
			TWindowsManager.getInstance().addWindow(alertwindow,parent);
			return alertwindow;
		}
	}
}